package cn.tedu.personal_patient.information.services.impl;


import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.personal_patient.information.dao.persist.repository.IUserRepository;
import cn.tedu.personal_patient.information.pojo.entity.Staff;
import cn.tedu.personal_patient.information.pojo.entity.User;

import cn.tedu.personal_patient.information.pojo.param.UserAvatarParam;
import cn.tedu.personal_patient.information.pojo.param.UserParam;
import cn.tedu.personal_patient.information.pojo.param.UserUpdateInfoParam;
import cn.tedu.personal_patient.information.pojo.vo.UserVO;
import cn.tedu.personal_patient.information.services.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserRepository userRepository;


    @Override
    public List<UserVO> listById(CurrentPrincipal currentPrincipal) {
        return userRepository.listById(currentPrincipal.getId());
    }

    @Override
    public void updateById(CurrentPrincipal currentPrincipal, UserParam userParam) {
        User user=new User();
        user.setId(currentPrincipal.getId());
        BeanUtils.copyProperties(userParam,user);
        int rows = userRepository.updateById(user);
        if (rows!=1){
            String message="更新失败，服务器忙，请稍后重试";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public void avatar(CurrentPrincipal currentPrincipal, UserAvatarParam userAvatarParam) {
        User user=new User();
        BeanUtils.copyProperties(userAvatarParam,user);
        user.setId(currentPrincipal.getId());
        int rows = userRepository.avatar(user);

        if (rows!=1){
            String message = "修改失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    /*@Override
    public void updatenNackName(Long id, String nackname) {
        User user = new User();
        user.setNickname(nackname);
        user.setId(id);
        int rows = userRepository.updateById(user);
        if (rows != 1) {
            String message = "修改昵称失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }


    @Override
    public void updateImgeUrl(Long id, String imgeUrl) {
        User user = new User();
        user.setId(id);
        user.setImgeUrl(imgeUrl);
        int rows = userRepository.updateById(user);
        if (rows != 1) {
            String message = "修改头像失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void updatePhone(Long id, String phone) {
        int count = userRepository.countByPhoneAndNotId(phone, id);
        if (count > 0) {
            String message = "修改手机号码失败，手机号码已经被占用！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        User user = new User();
        user.setId(id);
        user.setPhone(phone);
        int rows = userRepository.updateById(user);
        if (rows != 1) {
            String message = "修改手机号码失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void updateEmail(Long id, String email) {

        int count = userRepository.countByEmailAndNotId(email, id);
        if (count > 0) {
            String message = "修改电子邮箱失败，电子邮箱已经被占用！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        User user = new User();
        user.setId(id);
        user.setEmail(email);
        int rows = userRepository.updateById(user);
        if (rows != 1) {
            String message = "修改电子邮箱失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }*/
}
